package com.campus.counseling.controller;

import com.campus.counseling.entity.Appointment;
import com.campus.counseling.entity.ConsultationRecord;
import com.campus.counseling.service.AppointmentService;
import com.campus.counseling.service.ConsultationRecordService;
import com.campus.counseling.model.common.Result;

import com.campus.counseling.util.SecurityUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/dashboard/counselor")
@RequiredArgsConstructor
public class DashboardController {
    
    private final AppointmentService appointmentService;
    private final ConsultationRecordService recordService;
    
    @GetMapping("/statistics")
    public Result<Map<String, Object>> getCounselorStatistics() {
        Long counselorId = SecurityUtils.getUserId();
        Map<String, Object> statistics = new HashMap<>();
        
        // 获取今日预约数
        LocalDate today = LocalDate.now();
        List<Appointment> todayAppointments = appointmentService.getAppointmentsByDateRange(
            counselorId, today, today);
        
        // 获取昨日预约数进行比较
        LocalDate yesterday = today.minusDays(1);
        List<Appointment> yesterdayAppointments = appointmentService.getAppointmentsByDateRange(
            counselorId, yesterday, yesterday);
        
        // 计算趋势
        int trend = 0;
        if (!yesterdayAppointments.isEmpty()) {
            trend = (todayAppointments.size() - yesterdayAppointments.size()) * 100 
                   / yesterdayAppointments.size();
        }
        
        statistics.put("todayAppointments", todayAppointments.size());
        statistics.put("appointmentTrend", trend);
        statistics.put("completedConsults", recordService.getCompletedConsultsCount(counselorId));
        statistics.put("totalStudents", recordService.getServedStudentsCount(counselorId));
        
        // 获取本周每天的预约数量
        List<Integer> weeklyAppointments = appointmentService.getWeeklyAppointments();
        statistics.put("weeklyAppointments", weeklyAppointments);
        
        // 获取各类型咨询的数量统计
        List<Map<String, Object>> consultationTypes = appointmentService.getConsultationTypeStats();
        statistics.put("consultationTypes", consultationTypes);
        
        return Result.success(statistics);
    }
    
    @GetMapping("/schedule")
    public Result<List<Map<String, Object>>> getTodaySchedule() {
        Long counselorId = SecurityUtils.getUserId();
        LocalDate today = LocalDate.now();
        
        List<Appointment> appointments = appointmentService.getAppointmentsByDateRange(
            counselorId, today, today);
            
        List<Map<String, Object>> schedule = appointments.stream()
            .map(appointment -> {
                Map<String, Object> item = new HashMap<>();
                item.put("id", appointment.getId());
                item.put("studentName", appointment.getStudentName());
                item.put("studentAvatar", appointment.getStudentAvatar());
                item.put("appointmentTime", appointment.getAppointmentTime());
                item.put("status", appointment.getStatus());
                return item;
            })
            .collect(Collectors.toList());
            
        return Result.success(schedule);
    }
    
    @GetMapping("/todos")
    public Result<List<Map<String, Object>>> getTodoList() {
        Long counselorId = SecurityUtils.getUserId();
        List<Map<String, Object>> todos = new ArrayList<>();
        
        // 获取待确认的预约
        List<Appointment> pendingAppointments = appointmentService.getPendingAppointments(counselorId);
        
        // 获取待完成的咨询记录
        List<ConsultationRecord> pendingRecords = recordService.getPendingRecords(counselorId);
        
        // 转换为待办事项
        pendingAppointments.forEach(appointment -> {
            Map<String, Object> todo = new HashMap<>();
            todo.put("id", "apt_" + appointment.getId());
            todo.put("title", "待确认预约：" + appointment.getStudentName());
            todo.put("type", "urgent");
            todo.put("createTime", appointment.getCreateTime());
            todos.add(todo);
        });
        
        pendingRecords.forEach(record -> {
            Map<String, Object> todo = new HashMap<>();
            todo.put("id", "rec_" + record.getId());
            todo.put("title", "待完成咨询记录：" + record.getStudentName());
            todo.put("type", "normal");
            todo.put("createTime", record.getCreateTime());
            todos.add(todo);
        });
        
        return Result.success(todos);
    }
} 